﻿Imports paycircuit.com.google.iap



'''<summary>
'''This is a test class for InAppItemObjectTest and is intended
'''to contain all InAppItemObjectTest Unit Tests
'''</summary>
<TestClass()> <System.Runtime.InteropServices.GuidAttribute("2D5D30F5-6A43-41DE-8F47-52B7DBA4CE4D")> _
Public Class InAppItemObjectTest

    Const MY_SELLER_ID As String = "YOUR_SELLER_ID" ' "YOUR SELLER ID"
    Const MY_SELLER_SECRET As String = "YOUR_SELLER_SECRET" ' "YOUR SELLER SECRET"
    Const MY_POSTBACK_TYP As String = "google/payments/inapp/item/v1/postback/buy"
    Const THE_ISSUER As String = "Google"

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    'You can use the following additional attributes as you write your tests:
    '
    'Use ClassInitialize to run code before running the first test in the class
    '<ClassInitialize()>  _
    'Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    'End Sub
    '
    'Use ClassCleanup to run code after all tests in a class have run
    '<ClassCleanup()>  _
    'Public Shared Sub MyClassCleanup()
    'End Sub
    '
    'Use TestInitialize to run code before running each test
    '<TestInitialize()>  _
    'Public Sub MyTestInitialize()
    'End Sub
    '
    'Use TestCleanup to run code after each test has run
    '<TestCleanup()>  _
    'Public Sub MyTestCleanup()
    'End Sub
    '
#End Region


    '''<summary>
    '''A test for InAppItemObject Constructor
    '''</summary>
    <TestMethod()> _
    Public Sub InAppItemObjectConstructorTest()
        Dim itemname50 As String = "The item"
        Dim description100 As String = "the description"
        Dim itemprice As [Decimal] = 8.99D
        Dim iso3Currency As String = "USD"
        Dim privatedata200 As String = String.Empty
        Dim merchantid As String = "12345"
        Dim expMin60 As Nullable(Of Integer) = Nothing '55 70
        Dim target As InAppItemObject = New InAppItemObject(itemname50, description100, itemprice.ToString, iso3Currency, privatedata200, merchantid, expMin60)
        Assert.IsTrue(If(expMin60.HasValue, CBool(expMin60 <= 60), True), "expMin60 cannot be more than 60 minutes")
        Assert.IsTrue(If(expMin60.HasValue AndAlso expMin60 <= 60 AndAlso expMin60 > 0, CBool(target.exp - target.iat = expMin60 * 60), If(expMin60.HasValue AndAlso expMin60 > 0, CBool(target.exp - target.iat = 3600), True)))
        Assert.IsTrue(Not String.IsNullOrEmpty(target.request.name), "item name is required")
        Assert.IsTrue(Not String.IsNullOrEmpty(target.iss), "merchant id is required")
        Assert.IsTrue(IsNumeric(itemprice), "price is required")
        Assert.IsTrue(Not String.IsNullOrEmpty(target.request.currencyCode), "iso3Currency  is required")
    End Sub

    '''<summary>
    '''A test for InAppItemObject Constructor
    '''</summary>
    <TestMethod()> _
    <ExpectedException(GetType(Exception), "This should have thrown an exception - request property not set to instance of InAppItemRequestObject")>
    Public Sub InAppItemObjectConstructorExceptionTest()
        Dim target As InAppItemObject = New InAppItemObject()
        Assert.Inconclusive(String.Format("JSON with default values: {0}", JSONHelpers.dataContractToJSON(target)))
    End Sub

    '''<summary>
    '''A test for InAppItemObject Constructor
    '''</summary>
    <TestMethod()> _
    Public Sub InAppItemObjectConstructorTest2()
        Dim request As New InAppItemRequestObject() With {.name = "Gold Star", .description = "A shining badge of distinction", .price = "3.99", .currencyCode = RequestObject.SupportedCurrencies.GBP.ToString()}
        Dim target As InAppItemObject = New InAppItemObject(request)

        target.iss = MY_SELLER_ID
        ' target.response = New InAppItemResponseObject()
        Dim actual = JSONHelpers.dataContractToJSON(target)
        Assert.IsTrue(Not String.IsNullOrEmpty(actual))
        Assert.Inconclusive(String.Format("JSON with default values: {0}", actual))
    End Sub

    '''<summary>
    '''A test for InAppItemObject Constructor using named optional args
    '''Should handle future version changes coming from Google (i.e. v1, v2....)
    '''</summary>
    <TestMethod()> _
    Public Sub InAppItemObjectConstructorOptionalArgsTest()
        Dim target As InAppItemObject = New InAppItemObject("the name", "optional test", "9.99", "USD", Nothing, MY_SELLER_ID, Nothing, optionalTyp:="my typ")
        target.iss = "this is required"
        Dim actual = JSONHelpers.dataContractToJSON(target)
        Assert.IsTrue(Not String.IsNullOrEmpty(actual))
        Assert.Inconclusive(String.Format("JSON with default values: {0}", actual))
    End Sub

    '''<summary>
    '''Expected exception (response requires orderID)
    '''</summary>
    <TestMethod()> _
    <ExpectedException(GetType(Exception))>
    Public Sub InAppItemObjectConstructorExceptionTest1()
        Dim target As InAppItemObject = New InAppItemObject()
        target.request = New InAppItemRequestObject() With {.name = "foo"}
        target.response = New InAppItemResponseObject()
        Dim actual = JSONHelpers.dataContractToJSON(target)
    End Sub

    '''<summary>
    '''A test for aud
    '''</summary>
    <TestMethod()> _
    Public Sub audTest()
        Dim target As InAppItemObject = New InAppItemObject()
        Dim expected As String = "goog.payments.inapp.buyItem"
        Dim actual As String
        target.aud = expected
        actual = target.aud
        Assert.AreEqual(expected, actual)
    End Sub

    '''<summary>
    '''A test for exp
    '''</summary>
    <TestMethod()> _
    Public Sub expTest()
        Dim addminutes As Integer? = 10
        'default iat generated if not set is adjusted by -1 minute
        Dim expected As Nullable(Of Integer) = If(addminutes.HasValue, CInt((DateTime.UtcNow.AddMinutes(CDbl(addminutes - 1)).Ticks - 621355968000000000) / 10000000), Nothing)
        Dim target As InAppItemObject = New InAppItemObject() With {.exp = expected}
        Assert.IsTrue(If(addminutes.HasValue, CBool(addminutes <= 60), True), "expMin60 cannot be more than 60 minutes")
        Assert.IsTrue(If(addminutes.HasValue AndAlso addminutes <= 60, CBool(target.exp - target.iat = addminutes * 60), If(addminutes.HasValue, CBool(target.exp - target.iat = 3600), True)), "")
    End Sub

    '''<summary>
    '''A test for exp
    '''</summary>
    <TestMethod()> _
    <ExpectedException(GetType(Exception))>
    Public Sub expExceptionTest()
        Dim addminutes As Integer? = 70
        'default iat generated if not set is adjusted by -1 minute
        Dim _exp As Nullable(Of Integer) = If(addminutes.HasValue, CInt((DateTime.UtcNow.AddMinutes(CDbl(addminutes - 1)).Ticks - 621355968000000000) / 10000000), Nothing)
        Dim target As InAppItemObject = New InAppItemObject() With {.exp = _exp, .request = New InAppItemRequestObject() With {.name = "foo"}}
        Dim expected As String = JSONHelpers.dataContractToJSON(target)
    End Sub

    '''<summary>
    '''A test for iat
    '''</summary>
    <TestMethod()> _
    Public Sub iatTest()
        Dim target As InAppItemObject = New InAppItemObject()
        Dim current As Integer = CInt(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds)
        Dim actual As Integer = target.iat
        Assert.IsTrue(current > target.iat)
        If target.exp.HasValue Then
            Assert.IsTrue(CBool(target.exp > target.iat))
        End If
    End Sub

    '''<summary>
    '''A test for isoCurrencyCode
    '''</summary>
    <TestMethod()> _
    Public Sub isoCurrencyCodeTest()
        Dim target As InAppItemObject = New InAppItemObject()
        target.request = New InAppItemRequestObject()
        Dim expected As String = "USD"
        Dim actual As String
        target.request.currencyCode = expected
        actual = target.request.currencyCode
        Assert.AreEqual(expected, actual, False, "isoCurrencyCode required, USD")
    End Sub

    ''' <summary>
    ''' Test for character limits
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    <ExpectedException(GetType(System.ArgumentOutOfRangeException), "Exceeds maximum character limit")>
    Public Sub isoCurrencyCodeExceptionTest()
        Dim target As InAppItemObject = New InAppItemObject() With {.request = New InAppItemRequestObject() With {.currencyCode = "USD "}}
    End Sub

    '''<summary>
    '''A test for iss
    '''</summary>
    <TestMethod()> _
    Public Sub issTest()
        Dim target As InAppItemObject = New InAppItemObject() With {.iss = "my merchant id"}
        Assert.IsTrue(Not String.IsNullOrEmpty(target.iss), "iss is required")
    End Sub

    '''<summary>
    '''A test for itemDescription
    '''</summary>
    <TestMethod()>
    <ExpectedException(GetType(ArgumentOutOfRangeException))>
    Public Sub itemDescriptionExceptionTest()
        Dim target As InAppItemObject = New InAppItemObject() With {.request = New InAppItemRequestObject() With {.description = New System.Text.StringBuilder().Insert(0, "foo bar ", 13).ToString}}
        Assert.IsTrue(If(Not IsNothing(target.request.description), target.request.description.Length <= 200, True), "item description limit is 200 characters")
    End Sub

    '''<summary>
    '''A test for itemName
    '''</summary>
    <TestMethod()> _
    Public Sub itemNameTest()
        Dim target As InAppItemObject = New InAppItemObject() With {.request = New InAppItemRequestObject With {.name = "my widget"}}
        Assert.IsTrue(Not String.IsNullOrEmpty(target.request.name), "item name is required")
    End Sub

    '''<summary>
    '''A test for itemPrice
    '''</summary>
    <TestMethod()> _
    <ExpectedException(GetType(ArgumentOutOfRangeException))>
    Public Sub itemPriceExceptionTest()
        Dim target As InAppItemObject = New InAppItemObject() With {.request = New InAppItemRequestObject() With {.price = Nothing}}
        Assert.IsTrue(IsNumeric(target.request.price), "price is required")
    End Sub


    '''<summary>
    '''A test for sellerData
    '''</summary>
    <TestMethod()> _
    Public Sub sellerDataTest()
        Try
            Dim target As InAppItemObject = New InAppItemObject() With {.request = New InAppItemRequestObject() With {.sellerData = New System.Text.StringBuilder().Insert(0, "foo bar ", 25).ToString}}
            Assert.IsTrue(target.request.sellerData.Length <= 200, String.Format("{0} characters. Although optional, sellerData is limited to 200 characters", target.request.sellerData.Length))
            'Assert.Inconclusive(String.Format("{0} characters. Although optional, sellerData is limited to 200 characters", target.sellerData.Length))
        Catch ex As Exception
            Throw New AssertInconclusiveException(ex.Message)
        End Try


    End Sub
End Class
